gua_dict = {
    "乾" : {
        "乾" : ("111111", "乾为天", 5),
        "姤" : ("111110", "天风姤", 0),
        "遁" : ("111100", "天山遁", 1),
        "否" : ("111000", "天地否", 2),
        "观" : ("110000", "风地观", 3),
        "剥" : ("100000", "山地剥", 4),
        "晋" : ("101000", "火地晋", 3),
        "大有" : ("101111", "火天大有", 2),
    },
    
    "坤" : {
        "坤" : ("000000", "坤为地", 5),
        "复" : ("000001", "地雷复", 0),
        "临" : ("000011", "地泽临", 1),
        "泰" : ("000111", "地天泰", 2),
        "大壮" : ("001111", "雷天大壮", 3),
        "夬" : ("011111", "泽天夬", 4),
        "需" : ("010111", "水天需", 3),
        "比" : ("010000", "水地比", 2),
    },


    "震" : {
        "震" : ("001001", "震为雷", 5),
        "豫" : ("001000", "雷地豫", 0),
        "解" : ("001010", "雷水解", 1),
        "恒" : ("001110", "雷风恒", 2),
        "升" : ("000110", "地风升", 3),
        "井" : ("010110", "水风井", 4),
        "大过" : ("011110", "泽风大过", 3),
        "随" : ("011001", "泽雷随", 2),
    },

    "巽" : {
        "巽" : ("110110", "巽为风", 5),
        "小畜" : ("110111", "风天小畜", 0),
        "家人" : ("110101", "风火家人", 1),
        "益" : ("110001", "风雷益", 2),
        "无妄" : ("111001", "天雷无妄", 3),
        "噬嗑" : ("101001", "火雷噬嗑", 4),
        "颐" : ("100001", "山雷颐", 3),
        "盅" : ("100110", "山风盅", 2),
    },

    "兑" : {
        "兑" : ("011011", "兑为泽", 5),
        "困" : ("011010", "泽水困", 0),
        "萃" : ("011000", "泽地萃", 1),
        "咸" : ("011100", "泽山咸", 2),
        "蹇" : ("010100", "水山蹇", 3),
        "谦" : ("000100", "地山谦", 4),
        "小过" : ("001100", "雷山小过", 3),
        "归妹" : ("001011", "雷泽归妹", 2),
    },

    "艮" : {
        "艮" : ("100100", "艮为山", 5),
        "贲" : ("100101", "山火贲", 0),
        "大畜" : ("100111", "山天大畜", 1),
        "损" : ("100011", "山泽损", 2),
        "睽" : ("101011", "火泽睽", 3),
        "履" : ("111011", "天泽履", 4),
        "中孚" : ("110011", "风泽中孚", 3),
        "渐" : ("110100", "风山渐", 2),
    },

    "离" : {
        "离" : ("101101", "离为火", 5),
        "旅" : ("101100", "火山旅", 0),
        "鼎" : ("101110", "火风鼎", 1),
        "未济" : ("101010", "火水未济", 2),
        "蒙" : ("100010", "山水蒙", 3),
        "涣" : ("110010", "风水涣", 4),
        "讼" : ("111010", "天水讼", 3),
        "同人" : ("111101", "天火同人", 2),
    },

    "坎" : {
        "坎" : ("010010", "坎为水", 5),
        "节" : ("010011", "水泽节", 0),
        "屯" : ("010001", "水雷屯", 1),
        "既济" : ("010101", "水火既济", 2),
        "革" : ("011101", "泽火革", 3),
        "丰" : ("001101", "雷火丰", 4),
        "明夷" : ("000101", "地火明夷", 3),
        "师" : ("000010", "地水师", 2),
    },
}

# 金: 0, 水: 1, 木: 2, 火: 3, 土: 4
bagua_wuxing = {
    "乾":0,
    "坤":4,
    "坎":1,
    "离":3,
    "震":2,
    "巽":2,
    "兑":0,
    "艮":4
}

dizhi_wuxing = (-1, 1, 4, 2, 2, 4, 3, 3, 4, 0, 0, 4, 1)

def get_gua_data(name):
    data = None

    for k, v in gua_dict.items():
        if (name in v):
            data = v.get(name)
            break
    
    if (data is None):
        print("Error : Could not find " + name)

    return k, data

# 装六亲
def get_liuqin(gong, yaos):
    gong_wuxing = bagua_wuxing.get(gong)
    if gong_wuxing is None:
        print("Error : 找不到宫位五行，" + gong)

    lst = []
    for i in yaos:
        yao_wuxing = dizhi_wuxing[i]
        
        r = (yao_wuxing - gong_wuxing + 5) % 5
        print(i, yao_wuxing, gong, gong_wuxing, r)
        if (r == 1):
            lst.append("子孙")
        elif (r == 2):
            lst.append("妻财")
        elif (r == 4):
            lst.append("父母")
        elif (r == 3):
            lst.append("官鬼")
        elif (r == 0):
            lst.append("兄弟")

    print(lst)

    return lst

def get_yao_dizhi(yaos):
    waigua = yaos[0:3]
    neigua = yaos[3:]

    if waigua == "000":
        dizhi = [10, 12, 2]
    elif waigua == "001":
        dizhi = [11, 9, 7]
    elif waigua == "111":
        dizhi = [11, 9, 7]
    elif waigua == "010":
        dizhi = [1, 11, 9]
    elif waigua == "011":
        dizhi = [8, 10, 12]
    elif waigua == "110":
        dizhi = [4, 6, 8]
    elif waigua == "100":
        dizhi = [3, 1, 11]
    elif waigua == "101":
        dizhi = [6, 8, 10]

    if neigua == "111":
        dizhi.extend([5, 3, 1])
    elif neigua == "110":
        dizhi.extend([10, 12, 2])
    elif neigua == "100":
        dizhi.extend([9, 7, 5])
    elif neigua == "011":
        dizhi.extend([2, 4, 6])
    elif neigua == "101":
        dizhi.extend([12, 2, 4])
    elif neigua == "010":
        dizhi.extend([7, 5, 3])
    elif neigua == "001":
        dizhi.extend([5, 3, 1])
    elif neigua == "000":
        dizhi.extend([4, 6, 8])
    
    return dizhi

def rshift(lst, n):
    for i in range(n):
        t = lst.pop()
        lst.insert(0, t)
    return lst

def get_liushen(day):
    liushen = ["玄武", "白虎", "螣蛇", "勾陈", "朱雀", "青龙"]
    if day == "甲" or day == "乙":
        liushen_labels = liushen
    elif day == "丙" or day == "丁":
        liushen_labels = rshift(liushen, 1)
    elif day == "辛" or day == "庚":
        liushen_labels = rshift(liushen, 4)
    elif day == "壬" or day == "癸":
        liushen_labels = rshift(liushen, 5)
    elif day == "己" or day == "戊":
        liushen_labels = rshift(liushen, 3)


    return liushen_labels
